﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace VideoReflector.Video
{
    /// <summary>
    /// Helper class to generate bitmaps displaying string messages to be inserted as title frames
    /// in exported video clip files
    /// </summary>
    public class TitleFrameDrawer
    {
        #region Private fields

        private int _width;
        private int _height;
        private PixelFormat _pixelFormat;
        private Color _backColour;
        private Color _textColour;
        private string _fontFamily;
        private float _fontSize;
        private int _margin;

        #endregion

        #region Constructors

        public TitleFrameDrawer(int width, int height, PixelFormat pixelFormat, Color backColour, Color textColour,
                                string fontFamily, float fontSize, int margin)
        {
            _width = width;
            _height = height;
            _pixelFormat = pixelFormat;
            _backColour = backColour;
            _textColour = textColour;
            _fontFamily = fontFamily;
            _fontSize = fontSize;
            _margin = margin;
        }

        public TitleFrameDrawer(int width, int height, PixelFormat pixelFormat, Color backColour, Color textColour)
            : this(width, height, pixelFormat, backColour, textColour, "Arial", 16, 40) { }

        public TitleFrameDrawer(int width, int height, PixelFormat pixelFormat)
            : this(width, height, pixelFormat, Color.Black, Color.White) { }

        public TitleFrameDrawer(Bitmap exampleFrame, Color backColour, Color textColour)
            : this(exampleFrame.Width, exampleFrame.Height, exampleFrame.PixelFormat, backColour, textColour) { }

        public TitleFrameDrawer(Bitmap exampleFrame)
            : this(exampleFrame, Color.Black, Color.White) { }

        #endregion

        #region Public methods

        public Bitmap DrawFrame(string message)
        {
            var bitmap = new Bitmap(_width, _height, _pixelFormat);
            var graphics = Graphics.FromImage(bitmap);
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            graphics.FillRectangle(new SolidBrush(_backColour), 0, 0, _width, _height);
            var stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            stringFormat.LineAlignment = StringAlignment.Center;
            graphics.DrawString(message, new Font(_fontFamily, _fontSize), new SolidBrush(_textColour),
                new RectangleF(_margin / 2, _margin / 2, _width - _margin, _height - _margin), stringFormat);
            return bitmap;
        }

        #endregion // Public methods
    }
}
